package com.android.bandroid.plugins;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.bandroid.R;
import com.android.bandroid.common.utils.DisplayUtils;
import com.nineoldandroids.view.ViewHelper;

/**
 * 仿QQ5.0侧滑菜单
 *
 * Created by Kevin on 2015/11/17.
 */
@TargetApi(Build.VERSION_CODES.M)
public class SlidingMenu extends HorizontalScrollView {
    /**
     * 侧滑菜单距离右边距离
     */
    private int mMenuRightPadding = 50;
    /**
     * 屏幕宽度
     */
    private int mScreemWith;
    /**
     * 菜单宽度
     */
    private int mMenuWidth;
    /**
     * 菜单中布局宽度
     */
    private int mHalfMenuWidth;
    private boolean once;
    private Context mContext;
    /**
     * 滑动菜单是否打开
     */
    private boolean isOpen;
    /**
     * 左侧菜单
     */
    private ViewGroup menu;
    /**
     * 右侧区域
     */
    private ViewGroup content;
    /**
     * 右侧区域顶部左侧头像
     */
    private RelativeLayout leftLogoLlayout;

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mScreemWith = DisplayUtils.getScreenW(context);
    }

    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mScreemWith = DisplayUtils.getScreenW(context);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyleAttr, 0);
        int n = a.getIndexCount();
        for(int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.SlidingMenu_rightPadding:
                    //默认50DP
                    mMenuRightPadding = a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding,
                                    getResources().getDisplayMetrics()));
                    break;
            }
            a.recycle();
        }
    }



    /**
     * 控制位置
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(changed){
            //  将菜单隐藏
            this.scrollTo(mMenuWidth, 0);
            once = true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            // Up时，进行判断，如果显示区域大于菜单宽度一半则完全显示，否则隐藏
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if(scrollX > mHalfMenuWidth){
                    this.smoothScrollTo(mMenuWidth, 0);
                    isOpen = false;
                }else{
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 控制大小
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(!once){
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            menu = (ViewGroup) wrapper.getChildAt(0);     //  侧滑菜单
            content = (ViewGroup) wrapper.getChildAt(1);  //  home
            mMenuRightPadding = DisplayUtils.dip2px(mContext, mMenuRightPadding);
            mMenuWidth = mScreemWith - mMenuRightPadding;
            mHalfMenuWidth = mMenuWidth / 2;
            menu.getLayoutParams().width = mMenuWidth;
            content.getLayoutParams().width = mScreemWith;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 打开菜单
     */
    public void openMenu(){
        if(isOpen){
            return;
        }
        this.smoothScrollTo(0, 0);
        isOpen = true;
    }

    /**
     * 关闭菜单
     */
    public void closeMenu(){
        if(isOpen){
            this.smoothScrollTo(mMenuWidth, 0);
            isOpen = false;
        }
    }

    /**
     * 切换菜单状态
     */
    public void toggle(){
        if(isOpen){
            closeMenu();
        }else{
            openMenu();
        }
    }

    /**
     * 菜单打开时，点击收起菜单
     */
    public void toggleContent(){
        if(isOpen){
            closeMenu();
        }
    }

    public void setLeftLogoLlayout(RelativeLayout leftLogoLlayout) {
        this.leftLogoLlayout = leftLogoLlayout;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l * 1.0f / mMenuWidth;
        float leftScale = 1 - 0.3f * scale;
        float rightScale = 0.8f + scale * 0.2f;

        ViewHelper.setScaleX(menu, leftScale);
        ViewHelper.setScaleY(menu, leftScale);
        ViewHelper.setAlpha(menu, 0.6f + 0.4f * (1 - scale));
        ViewHelper.setTranslationX(menu, mMenuWidth * scale * 0.6f);

        ViewHelper.setPivotX(content, 0);
        ViewHelper.setPivotY(content, content.getHeight() / 2);
        ViewHelper.setScaleX(content, rightScale);
        ViewHelper.setScaleY(content, rightScale);

        ViewHelper.setAlpha(leftLogoLlayout, scale);
    }
}
